package org.gdkm.controller;

import com.github.pagehelper.PageInfo;
import org.gdkm.domin.TbUser;
import org.gdkm.service.impl.AdminServiceImpl;
import org.gdkm.util.BaseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;


import java.util.List;


@RestController
public class LoginController {

    @Autowired
    AdminServiceImpl adminServiceimpl;

    @GetMapping("hello")
    @ResponseBody
    public TbUser hello(String username, String password) {
        TbUser login = adminServiceimpl.login(username, password);
        System.out.println("login = " + login);
        return login;
    }

    @GetMapping("world")
    @ResponseBody
    public String world() {
        TbUser tbUser = new TbUser();
        tbUser.setUsername("tom");
        tbUser.setPassword("1234");
        adminServiceimpl.register(tbUser);
        return "success";
    }

    @GetMapping("login")
    public BaseResult login(String username, String password) {
        return adminServiceimpl.checklogin(username, password);
    }

    @GetMapping("page/{pageNumber}/{pageSize}")
    public BaseResult page(@PathVariable("pageNumber") int pageNumber,
                           @PathVariable("pageSize") int pageSize,
                           @PathVariable(required = false) TbUser tbUser){
        PageInfo<TbUser> page = adminServiceimpl.page(pageNumber, pageSize, tbUser);
        List<TbUser> list = page.getList();
        BaseResult.Cursor cursor=new BaseResult.Cursor();
        cursor.setTotal(Long.valueOf(page.getTotal()).intValue());
        cursor.setOffset(page.getPageNum());
        cursor.setLimit(page.getPageSize());
        return BaseResult.Ok(list,cursor);
    }
}

